<template>
  <a-modal
    :title="title"
    :width="1070"
    :visible="visible"
    :confirmLoading="loading"
    :maskClosable="false"
    :destroyOnClose="true"
    :footer="true"
    @cancel="cancel"
  >
    <a-spin :spinning="loading">
      <div class="table-operator">
        <a-button type="primary" @click="handleAdd">添加等级</a-button>
<!--        <SearchForm ref="SearchForm" @handleSubmit="handleSeachSubmit" />-->
      </div>
      <s-table
        ref="table"
        rowKey="qt_id"
        :loading="isLoading"
        :columns="columns"
        :data="loadData"
        :pageSize="15"
        :scroll="{y:500}"
      >
        <div slot="status" slot-scope="text, item">
          <a-tag color="green" v-if="item.status==0">启用</a-tag>
          <a-tag color="red" v-if="item.status==1">停用</a-tag>
        </div>
        <div class="actions" slot="action" slot-scope="text, item">
          <a @click="handleEdit(item)">编辑</a>
          <a-popconfirm
            title="确认删除当前记录？"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDelete(item)"
          >
            <a href="#">删除</a>
          </a-popconfirm>
        </div>
      </s-table>
      <SaveForm :subject_id="queryParam.subject_id" ref="SaveForm" @handleSubmit="handleRefresh" />
    </a-spin>
  </a-modal>
</template>
<script>
import * as Api from '@/api/subject/level'
import { STable } from '@/components'
import SearchForm from './modules/SearchForm'
import SaveForm from './modules/SaveForm'

export default {
  name: 'TopicListModel',
  components: { SearchForm, SaveForm, STable },
  data () {
    return {
      title: '题目列表',
      queryParam: {},
      visible: false,
      loading: false,
      isLoading: false,
      vehicle_id: '',
      columns: [
        {
          title: '等级ID',
          dataIndex: 'subject_level_id',
          align: 'center',
          width: 60
        },
        {
          title: '等级名称',
          dataIndex: 'level_name',
          align: 'center'
        },
        {
          title: '绑定题库',
          dataIndex: 'question.question_name',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      currVrId: '',
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  methods: {
    open ({ subject_name, subject_id }) {
      this.title = subject_name + ' - 等级列表'
      this.queryParam.subject_id = subject_id
      this.visible = true
    },
    cancel () {
      this.visible = false
    },

    /**
     * 添加
     */
    handleAdd () {
      this.$refs.SaveForm.add()
    },

    /**
     * 编辑
     */
    handleEdit (item) {
      this.$refs.SaveForm.edit(item)
    },

    handleDelete (item) {
      Api.deleted({ apiId: item['subject_level_id'] })
        .then((result) => {
          this.$message.success(result.message, 1.5)
          this.handleRefresh()
        })
    },

    /**
     * 搜索
     */
    handleSeachSubmit (value) {
      this.queryParam = {
        ...this.queryParam,
        ...value
      }
      this.handleRefresh(true)
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    }
  }
}
</script>
<style scoped lang="less">

</style>
